import java.util.LinkedList;
import java.util.Queue;

/**
 * 队列实现栈
 */

public class MyStack {
    private Queue<Integer> qu1;
    private Queue<Integer> qu2;

    public MyStack() {
        qu1 = new LinkedList<>();
        qu2 = new LinkedList<>();
    }

    //入到不为空的队列中
    public void offer(int val) {
        if (!qu1.isEmpty()) {
            qu1.offer(val);
        } else if (!qu2.isEmpty()) {
            qu2.offer(val);
        } else {
            qu1.offer(val);
        }
    }

    public int poll() {
        if (empty()) {
            return -1;
        }
        if (!qu1.isEmpty()) {
            int size = qu1.size();
            for(int i = 0; i < size-1; i++) {
                qu2.offer(qu1.poll());
            }
            return qu1.poll();
        } else {
            int size = qu2.size();
            for (int i = 0; i < size - 1; i++) {
                qu1.offer(qu2.poll());
            }
            return qu2.poll();
        }

    }

    public int peek() {
        if (empty()) {
            return -1;
        }
        if (!qu1.isEmpty()) {
            int tmp = -1;
            int size = qu1.size();
            for(int i = 0; i < size; i++){
                tmp = qu1.poll();
                qu2.offer(tmp);
            }
            return tmp;
        } else {
            int size = qu2.size();
            int tmp = -1;
            for(int i = 0; i < size; i++) {
                tmp = qu2.poll();
                qu1.offer(tmp);
            }
            return tmp;
        }
    }

    public boolean empty() {
        return qu1.isEmpty() && qu2.isEmpty();
    }
}
